#include "widget.h"
#include "ui_widget.h"
#include<QLabel>
#include<QAction>
#include<QString>
#include<QImage>
#include<QImageReader>
#include<QPixmap>
#include<QStringList>
#include<QMessageBox>
#include <QTcpServer>
#include <QTcpSocket>
#include<QStatusBar>
#include<QLabel>
#include"dialog.cpp"
#include"mainwindow.cpp"
#include"tcpserver.cpp"
#include<QDebug>
#include<map>
#include<string>
#include <QSqlQuery>
#include <QtSql>
#include <qsqldatabase.h>
#include"ticket.cpp"
#include<QTcpServer>
#include<QTcpSocket>
#include <synchapi.h>
using namespace std;
QStringList qs1,qs2,qs3,qs4,qs5,qs6,qs,qs14;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","1");
QStringList  qs0;
map<string,int>m1;
map<int,string>m2;
int dir[200][200];
int path[200][200];
QString getname(int index)
{
    for(auto it=m1.begin();it!=m1.end();it++)
    {
        if(it->second==index)
        {
            return QString::fromStdString(it->first);
            break;
        }
    }
    return "error";
}
void print(int i,int j,QStringList* qsn)
{
    if(i==j) return; //分段到同一点，停止
    if(path[i][j]==0)
    {
        *qsn<<"->"<<getname(j);
    }
    else
    {
        print(i,path[i][j],qsn);
        print(path[i][j],j,qsn);
    }
}
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lab->setScaledContents(true);
    QImageReader::setAllocationLimit(320);
    QString s=":/resource/image3.jpg";
    QImage img;
    img.load(s);
    QPixmap p=QPixmap::fromImage(img);
    p.scaled(ui->lab->size());
    ui->lab->setPixmap(p);
    qs1<<"河南工业大学站"<<"郑大科技园站"<<"郑州大学站"<<"梧桐街站"<<"兰寨站"<<"铁炉站"<<"市民中心站"<<"西流湖站"<<"西三环站"<<"秦岭路站"<<"五一公园站"<<"碧沙岗站"<<"绿城广场站"<<"医学院站"<<"郑州火车站"<<"二七广场站"<<"人民路站"<<"紫荆山站"<<"燕庄站"<<"民航路站"<<"会展中心站"<<"黄河南路站"<<"农业南路站"<<"东风南路站"<<"郑州东站"<<"博学路站"<<"市体育中心站"<<"龙子湖站"<<"文苑北路站"<<"河南大学新区站";

    qs2<<"贾河站"<<"惠济区政府站"<<"毛庄站"<<"黄河迎宾馆站"<<"金洼站"<<"金达路站"<<"刘庄站"<<"柳林站"<<"沙门站"<<"北三环站"<<"东风路站"<<"关虎屯站"<<"黄河路站"<<"紫荆山站"<<"东大街站"<<"陇海东路站"<<"二里岗站"<<"南五里堡站"<<"花寨站"<<"南三环站"<<"站马屯站"<<"南四环站"<<"十八里河站" << "沙窝李站" << "双湖大道站" << "小乔站" << "华南城西站" << "华南城站" << "华南城东站" << "孟庄站" << "港区北站" << "康平湖站" << "兰河公园站" << "恩平湖站" << "综合保税区站" << "新郑机场站" <<"3号航站楼站"<< "机场东站" << "港区会展站" << "郑州航空港站";

    qs3 <<"省体育中心站"<<"王砦站"<<"兴隆铺站"<<"同乐站"<<"南阳新村站"<<"海滩寺站"<<"大石桥站"<<"人民公园站"<<"二七广场站"<<"西大街站"<<"东大街站"<<"郑州文庙站"<<"博览中心站"<<"凤凰台站"<<"东十里铺站"<<"通泰路站"<<"西周站"<<"东周站"<<"省骨科医院站"<<"圃田西站"<<"营岗站"<<"潮河站"<<"滨河新城站"<<"司赵站"<<"滨河新城南站";

    qs4<< "老鸦陈站"<<"省体育中心站"<<"北二十里铺站"<<"丰庆路站"<<"张家村站"<<"陈寨东站"<<"沙门站"<<"杨君刘站"<<"森林公园北站"<<"清华附中站"<<"龙湖中环北站"<<"龙湖北站"<<"金融岛北站"<<"金融岛南站"<<"龙湖南站"<<"龙湖中环南站"<<"农业东路站"<<"中央商务区站"<<"会展中心站"<<"商鼎路站"<<"东十里铺站"<<"货站街站"<<"七里河站"<<"果树所站"<<"金岱站"<<"姚庄站"<<"郎庄站";

    qs5<< "航海广场站" <<"七里河站" <<"中原福塔站" <<"福塔东站"<< "经开中心广场站" << "经北二路站" << "省骨科医院站"<< "康宁街站" << "郑州东站" << "金水东路站" << "祭城站"<< "儿童医院·颐和医院站" << "中央商务区站" << "众意西路站"<< "姚砦站" << "省人民医院站" <<"黄河路站" <<"郑州人民医院站" << "海滩寺站" << "沙口路站" << "月季公园站" << "五一公园站" << "市中心医院站" << "陇海西路站" << "桐淮站" << "后河芦站" << "齐礼阎站" << "市第二人民医院站" << "京广南路站" << "冯庄站" << "南五里堡站" << "城东南路站";

    qs6<<"桐淮站"<<"二里岗站"<<"博览中心站" <<"燕庄站"<<"姚砦站"<<"清华附中站"<< "常庄站" << "奥体中心站" << "奥体中心西站" << "金桢路站" << "河南建院站" << "马寨站" << "安宁路站" << "洞林寺站" << "洞林湖站" << "贾峪站";

    qs14 <<"须水站" <<"铁炉站" <<"市民大道站"<< "市委党校站" << "奥体中心站" << "莲湖站";
    qs<<"郑大科技园站"<<"河南工业大学站"<<"郑州大学站"<<"梧桐街站"<<"兰寨站"<<"铁炉站"<<"市民中心站"<<"西流湖站"<<"西三环站"<<"秦岭路站"<<"五一公园站"<<"碧沙岗站"<<"绿城广场站"<<"医学院站"<<"郑州火车站"<<"二七广场站"<<"人民路站"<<"紫荆山站"<<"燕庄站"<<"民航路站"<<"会展中心站"<<"黄河南路站"<<"农业南路站"<<"东风南路站"<<"郑州东站"<<"博学路站"<<"市体育中心站"<<"龙子湖站"<<"文苑北路站"<<"河南大学新区站";
    ui->comboBox_3->addItems(qs1);
    ui->comboBox_4->addItems(qs);
    this->d=new Dialog(this);
    this->m=new MainWindow(this);
    this->c=new Tcpserver(this);
    this->t=new Ticket();
    t->hide();
    c->hide();
    d->hide();
    m->hide();
    connect(this,&Widget::change_label,d,&Dialog::do_change_label);
    connect(c,&Tcpserver::success,this,&Widget::do_it);
    connect(t,&Ticket::del,this,&Widget::do_del);
    qs0=qs1+qs2+qs3+qs4+qs5+qs6+qs14;
    int a=0,b=0;
    for(int i=0;i<176;i++)
    {
        if(m1.count(qs0[i].toStdString())==0)
        {
            m1.insert(make_pair(qs0[i].toStdString(),a++));
            m2.insert(make_pair(b++,qs0[i].toStdString()));
        }
        else;
    }
    for(int i=0;i<qs1.size();i++)
    {
        dir[m1[qs1[i].toStdString()]][m1[qs1[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs1[i].toStdString()]][m1[qs1[i+1].toStdString()]]=1;
        }
        else if(i==qs1.size()-1)
        {
            dir[m1[qs1[i].toStdString()]][m1[qs1[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs1[i].toStdString()]][m1[qs1[i+1].toStdString()]]=1;
            dir[m1[qs1[i].toStdString()]][m1[qs1[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs2.size();i++)
    {
        dir[m1[qs2[i].toStdString()]][m1[qs2[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs2[i].toStdString()]][m1[qs2[i+1].toStdString()]]=1;
        }
        else if(i==qs2.size()-1)
        {
            dir[m1[qs2[i].toStdString()]][m1[qs2[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs2[i].toStdString()]][m1[qs2[i+1].toStdString()]]=1;
            dir[m1[qs2[i].toStdString()]][m1[qs2[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs3.size();i++)
    {
        dir[m1[qs3[i].toStdString()]][m1[qs3[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs3[i].toStdString()]][m1[qs3[i+1].toStdString()]]=1;
        }
        else if(i==qs3.size()-1)
        {
            dir[m1[qs3[i].toStdString()]][m1[qs3[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs3[i].toStdString()]][m1[qs3[i+1].toStdString()]]=1;
            dir[m1[qs3[i].toStdString()]][m1[qs3[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs4.size();i++)
    {
        dir[m1[qs4[i].toStdString()]][m1[qs4[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs4[i].toStdString()]][m1[qs4[i+1].toStdString()]]=1;
        }
        else if(i==qs4.size()-1)
        {
            dir[m1[qs4[i].toStdString()]][m1[qs4[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs4[i].toStdString()]][m1[qs4[i+1].toStdString()]]=1;
            dir[m1[qs4[i].toStdString()]][m1[qs4[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs5.size();i++)
    {
        dir[m1[qs5[i].toStdString()]][m1[qs5[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs5[i].toStdString()]][m1[qs5[i+1].toStdString()]]=1;
            dir[m1[qs5[i].toStdString()]][m1[qs5[qs5.size()-1].toStdString()]]=1;
        }
        else if(i==qs5.size()-1)
        {
            dir[m1[qs5[i].toStdString()]][m1[qs5[i-1].toStdString()]]=1;
            dir[m1[qs5[i].toStdString()]][m1[qs5[0].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs5[i].toStdString()]][m1[qs5[i+1].toStdString()]]=1;
            dir[m1[qs5[i].toStdString()]][m1[qs5[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs6.size();i++)
    {
        dir[m1[qs6[i].toStdString()]][m1[qs6[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs6[i].toStdString()]][m1[qs6[i+1].toStdString()]]=1;
        }
        else if(i==qs6.size()-1)
        {
            dir[m1[qs6[i].toStdString()]][m1[qs6[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs6[i].toStdString()]][m1[qs6[i+1].toStdString()]]=1;
            dir[m1[qs6[i].toStdString()]][m1[qs6[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<qs14.size();i++)
    {
        dir[m1[qs14[i].toStdString()]][m1[qs14[i].toStdString()]]=0;
        if(i==0)
        {
            dir[m1[qs14[i].toStdString()]][m1[qs14[i+1].toStdString()]]=1;
        }
        else if(i==qs14.size()-1)
        {
            dir[m1[qs14[i].toStdString()]][m1[qs14[i-1].toStdString()]]=1;
        }
        else
        {
            dir[m1[qs14[i].toStdString()]][m1[qs14[i+1].toStdString()]]=1;
            dir[m1[qs14[i].toStdString()]][m1[qs14[i-1].toStdString()]]=1;
        }
    }
    for(int i=0;i<153;i++)
    {
        for(int j=0;j<153;j++)
        {
            if(i!=j&&dir[i][j]==0)
            {
                dir[i][j]=10000;
            }
        }
    }
    for(int k=0;k<153;k++)
    {
        for(int i=0;i<153;i++)
            for(int j=0;j<153;j++)
                if(dir[i][j]>dir[i][k]+dir[k][j])
                {
                    dir[i][j]=dir[i][k]+dir[k][j];
                    path[i][j]=k;
                }
    }

    db.setHostName(m->getLocalIP());
    db.setUserName("root");
    db.setPort(3306);
    db.setPassword("?yuban233?");
    db.setDatabaseName("test");
    // auto sql = QSqlDatabase::database("1");
    // QString ss= "CREATE DATABASE IF NOT EXISTS test1";
    // sql.exec(ss);
    if(!db.open())
    {
        qDebug()<<"connect failed";
        qDebug() << db.lastError();//.databaseText()  输出错误信息
        QString dlgTitle= "warning 消息框";
        QString strInfo = "未与数据库连接";
        QMessageBox::warning(this, dlgTitle, strInfo);
    }else
    {
    }

}


void Widget:: do_it()
{
    ui->label_3->setText("连接成功");
}
Widget::~Widget()
{
    delete ui;
}

void Widget::on_comboBox_currentIndexChanged(int index)
{
    ui->comboBox_3->clear();
    switch(index)
    {
    case 0:
        ui->comboBox_3->addItems(qs1);
        break;
    case 1:
        ui->comboBox_3->addItems(qs2);
        break;
    case 2:
        ui->comboBox_3->addItems(qs3);
        break;
    case 3:
        ui->comboBox_3->addItems(qs4);
        break;
    case 4:
        ui->comboBox_3->addItems(qs5);
        break;
    case 5:
        ui->comboBox_3->addItems(qs6);
        break;
    case 6:
        ui->comboBox_3->addItems(qs14);
    default:
        break;
    }
}

void Widget::on_comboBox_2_currentIndexChanged(int index)
{
    ui->comboBox_4->clear();
    switch(index)
    {
    case 0:
        ui->comboBox_4->addItems(qs1);
        break;
    case 1:
        ui->comboBox_4->addItems(qs2);
        break;
    case 2:
        ui->comboBox_4->addItems(qs3);
        break;
    case 3:
        ui->comboBox_4->addItems(qs4);
        break;
    case 4:
        ui->comboBox_4->addItems(qs5);
        break;
    case 5:
        ui->comboBox_4->addItems(qs6);
        break;
    case 6:
        ui->comboBox_4->addItems(qs14);
    default:
        break;
    }
}

QStringList qss,qss1,qss2,qss0;
void Widget::on_pushButton_clicked()
{
    QString dlgTitle= "Question 消息框";
    QString strInfo = "确认要出票吗";
    QMessageBox::StandardButton  defaultBtn= QMessageBox::NoButton;
    QMessageBox::StandardButton result;
    result=QMessageBox::question(this, dlgTitle, strInfo,
                            QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                            defaultBtn);
    if (result == QMessageBox::Yes)
    {
        if(ui->label_3->text()!="连接成功")
        {
            QString dlgTitle= "warning 消息框";
            QString strInfo = "未与服务器连接";
            QMessageBox::warning(this, dlgTitle, strInfo);
        }
        else
        {
            d->show();
            QStringList qs1;
            qs1<<ui->comboBox->currentText()<<ui->comboBox_3->currentText()<<ui->comboBox_2->currentText()<<ui->comboBox_4->currentText();
            QSqlQuery query(db);
            QString str=QString("create table ticket( way varchar(300),start varchar(20),end varchar(20),time varchar(25))  DEFAULT CHARSET utf8 COLLATE utf8_general_ci");
            if(!query.exec(str))
                qDebug()<< " error: "<<query.lastError();
            else
                qDebug()<< "创建表成功！"<<str;
            QStringList l1;
            l1<<ui->comboBox_3->currentText();
            print(m1[ui->comboBox_3->currentText().toStdString()],m1[ui->comboBox_4->currentText().toStdString()],&l1);
            QString q1=l1[0],q2=l1[l1.size()-1],q3=l1.join(' ');
            QSqlQuery query1(db);
        query1.prepare("insert into ticket(way,start,end,time) values(:way,:start,:end,:time)");
        query1.bindValue(":way", q3);
        query1.bindValue(":start", q1);
        query1.bindValue(":end", q2);
        query1.bindValue(":time",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        if(!query1.exec())
        {
            QMessageBox::critical(this,"提示","新增失败！"+query1.lastError().text());
            return;
        }
        t->flush();
            emit change_label(qs1);
        }
    }
    else if(result == QMessageBox::No)
    {

    }
    else if(result == QMessageBox::Cancel)
    {

    }
    else
    {

    }
}
void Widget::do_del(QString q)
{
    QString sql = QString("delete from ticket where time = '%1'").arg(q);
    QSqlQuery query(db);
        if(!query.exec(sql))
        {
            QMessageBox::critical(this,"提示","删除失败！"+query.lastError().text());
            return;
        }
        t->flush();
}
void Widget::on_pushButton_2_clicked()
{
    m->show();
    c->show();
    c->do_connect();
    m->do_connect(ui->comboBox->currentText()+ui->comboBox_3->currentText()+ui->comboBox_2->currentText()+ui->comboBox_4->currentText());

}


void Widget::on_comboBox_3_currentTextChanged(const QString &arg1)
{
    if(arg1==ui->comboBox_4->currentText())
    {
        QString dlgTitle= "warning 消息框";
        QString strInfo = "起始站和终点站相同";
        QMessageBox::warning(this, dlgTitle, strInfo);
        int index=ui->comboBox_3->currentIndex();
        if(index>1)
            ui->comboBox_3->setCurrentIndex(index-1);
        else
            ui->comboBox_3->setCurrentIndex(index+1);
    }
}
void Widget::on_comboBox_4_currentTextChanged(const QString &arg1)
{
    if(arg1==ui->comboBox_3->currentText())
    {
        QString dlgTitle= "warning 消息框";
        QString strInfo = "起始站和终点站相同";
        QMessageBox::warning(this, dlgTitle, strInfo);
        int index=ui->comboBox_4->currentIndex();
        if(index>1)
            ui->comboBox_4->setCurrentIndex(index-1);
        else
            ui->comboBox_4->setCurrentIndex(index+1);
    }
}
void Widget::on_pushButton_3_clicked()
{
    t->flush();
    t->show();
}

